package com.diankangwo.factory.controller;

import com.diankangwo.common.annotation.SysLog;
import com.diankangwo.common.utils.PageUtils;
import com.diankangwo.common.utils.Query;
import com.diankangwo.common.utils.R;
import com.diankangwo.common.validator.ValidatorUtils;
import com.diankangwo.common.validator.group.AddGroup;
import com.diankangwo.common.validator.group.UpdateGroup;
import com.diankangwo.factory.dao.TdClassificationMapper;
import com.diankangwo.factory.dao.TdSpeciesMapper;
import com.diankangwo.factory.entity.*;
import com.diankangwo.factory.service.*;
import com.diankangwo.sys.controller.AbstractController;
import com.diankangwo.sys.entity.SysUserEntity;
import com.diankangwo.sys.service.SysUserService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 分类
 * Created by wuye on 2017/9/11.
 */
@RestController
@RequestMapping("/fac/type")
public class TdTypeController extends AbstractController {

    @Autowired
    private TypeService typeService;

    @Autowired
    private TdClassificationService tdClassificationService;

    @Autowired
    private TdClassificationMapper tdClassificationMapper;

    @Autowired
    private TdSpeciesService tdSpeciesService;

    @Autowired
    private TdSpeciesMapper tdSpeciesMapper;

    @Autowired
    private TdCommodityService tdCommodityService;

    @Autowired
    private TdFactoryService tdFactoryService;

    @Autowired
    private SysUserService sysUserService;


    /**
     * 获取一级分类列表
     */
    @ApiOperation(value = "获取一级分类列表", notes = "")
    @ApiImplicitParam(name = "params", value = "查询条件", required = true, dataType = "Map<String,Object>")
    @RequestMapping("/list")
    @RequiresPermissions("fac:type:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<TdType> typeList = typeService.queryList(query);
        int total = typeService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(typeList, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }

    /**
     * 新增二级
     */
    @ApiOperation(value = "新增二级", notes = "")
    @ApiImplicitParam(name = "tdClassification", value = "二级分类", required = true, dataType = "TdClassification")
    @RequestMapping("/addNext")
    @RequiresPermissions("fac:type:addNext")
    public R addNext(@RequestBody TdClassification tdClassification) {
        ValidatorUtils.validateEntity(tdClassification, AddGroup.class);
        if (tdClassification.getId() == null) {
            tdClassificationService.save(tdClassification);
        } else {
            tdClassificationMapper.updateByPrimaryKey(tdClassification);
        }

        return R.ok();
    }

    /**
     * 新增三级分类
     *
     * @return
     */
    @ApiOperation(value = "新增三级", notes = "")
    @ApiImplicitParam(name = "tdClassification", value = "三级级分类", required = true, dataType = "TdClassification")
    @RequestMapping("/addNext2")
    @RequiresPermissions("fac:type:addNext2")
    public R addNedx2(@RequestBody TdSpecies tdSpecie) {
        ValidatorUtils.validateEntity(tdSpecie, AddGroup.class);
        if (tdSpecie.getId() == null) {
            tdSpeciesService.save(tdSpecie);
        } else {
            tdSpeciesMapper.updateByPrimaryKey(tdSpecie);
        }
        return R.ok();
    }


    /**
     * 查看二级
     */
    @ApiOperation(value = "查看二级", notes = "")
    @ApiImplicitParam(name = "typeId", value = "一级分类id", required = true, dataType = "Integer")
    @RequestMapping("/findNext")
    @RequiresPermissions("fac:type:findNext")
    public R findNext(int typeId) {
        List<TdClassification> tdClassificationList = tdClassificationService.queryByTpesId(typeId);
        return R.ok().put("tdClassificationList", tdClassificationList);
    }

    /**
     * 查看三级
     */
    @ApiOperation(value = "查看三级", notes = "")
    @ApiImplicitParam(name = "claId", value = "二级分类id", required = true, dataType = "Integer")
    @RequestMapping("/findNext2")
    @RequiresPermissions("fac:type:findNext2")
    public R findNext2(int claId) {
        List<TdSpecies> tdSpeciesList = tdSpeciesService.queryByClaId(claId);
        return R.ok().put("tdClassificationList", tdSpeciesList);
    }


    /**
     * 获取单个一级分类详细
     */
    @ApiOperation(value = "获取单个一级分类详细", notes = "")
    @ApiImplicitParam(name = "typeId", value = "id", required = true, dataType = "Integer")
    @RequestMapping("/typeById")
    @RequiresPermissions("fac:type:typeById")
    public R typeById(int typeId) {
        //查询列表数据
        TdType type = typeService.queryById(typeId);
        return R.ok().put("type", type);
    }

    /**
     * 按ID查看二级
     */
    @ApiOperation(value = "按ID查看二级", notes = "")
    @ApiImplicitParam(name = "classificaitionId", value = "二级分类id", required = true, dataType = "Integer")
    @RequestMapping("/classById")
    @RequiresPermissions("fac:type:classById")
    public R classById(int classificaitionId) {
        TdClassification tdClassificationList = tdClassificationService.selectByPrimaryKey(classificaitionId);
        return R.ok().put("tdClassificationList", tdClassificationList);
    }

    /**
     * 按ID查看三级
     */
    @ApiOperation(value = "按ID查看三级", notes = "")
    @ApiImplicitParam(name = "speId", value = "三级分类id", required = true, dataType = "Integer")
    @RequestMapping("/speById")
    @RequiresPermissions("fac:type:speById")
    public R speById(int speId) {
        TdSpecies tdSpeciesList = tdSpeciesService.selectByPrimaryKey(speId);
        return R.ok().put("tdClassificationList", tdSpeciesList);
    }

    /**
     * 转移商品
     */


    /**
     * 一级分类删除
     */
    @ApiOperation(value = "删除", notes = "")
    @ApiImplicitParam(name = "typeId", value = "一级分类id", required = true, dataType = "Integer")
    @RequestMapping("/delete")
    @RequiresPermissions("fac:type:delete")
    public R delete(int typeId) {
        if (tdCommodityService.queryByTypeId(typeId).size() == 0) {
            //删除一级
            typeService.deleteById(typeId);
            List<TdClassification> tdClassification = tdClassificationService.queryByTpesId(typeId);
            int clasId = -1;
            if (tdClassification.size() > 0) {
                clasId = tdClassification.get(0).getId();
            }
            //批量删除二级
            tdClassificationService.deleteBatch(typeId);
            //批量删除三级
            if (clasId != -1) {
                tdSpeciesService.deleteBatch(clasId);
            }
        } else {
            return R.error(2, "分类下有商品存在，无法删除！");
        }
        return R.ok();
    }

    /**
     * 二级分类删除
     */
    @ApiOperation(value = "删除", notes = "")
    @ApiImplicitParam(name = "claId", value = "二级级分类id", required = true, dataType = "Integer")
    @RequestMapping("/deleteCla")
    @RequiresPermissions("fac:type:deleteCla")
    public R deleteCla(int claId) {
        List<TdCommodity> commodityList = tdCommodityService.queryByClassId(claId);
        if (commodityList.size() == 0) {
            //删除二级分类
            tdClassificationService.delete(claId);
            List<TdSpecies> tdClassificationList = tdSpeciesService.queryByClaId(claId);
            TdFactory tdFactory = tdFactoryService.selectByClassId(claId);
            if(tdFactory  != null){
                tdFactoryService.delete(tdFactory.getId());
                List<SysUserEntity> sysUserEntityList = sysUserService.queryByFactoryId(tdFactory.getId());
                for (int i = 0; i < sysUserEntityList.size(); i++) {
                    SysUserEntity sysUserEntity = sysUserEntityList.get(i);
                    sysUserEntity.setTdFactoryid(0);
                    sysUserService.update2(sysUserEntity);

                }
            }
            for (int i = 0; i < tdClassificationList.size(); i++) {
                //删除三级分类
                tdSpeciesService.deleteBatch(tdClassificationList.get(i).getId());
            }
        } else {
            return R.error(2, "分类下有商品存在，无法删除！");
        }

        return R.ok();
    }

    /**
     * 三级分类删除
     */
    @ApiOperation(value = "删除", notes = "")
    @ApiImplicitParam(name = "speId", value = "三级级分类id", required = true, dataType = "Integer")
    @RequestMapping("/deleteSpe")
    @RequiresPermissions("fac:type:deleteSpe")
    public R deleteSpe(int speId) {
        if (tdCommodityService.queryBySpeId(speId).size() == 0) {
            //删除三级分类
            tdSpeciesService.delete(speId);
        } else {
            return R.error(2, "分类下有商品存在，无法删除！");
        }
        return R.ok();
    }


    /**
     * 编辑
     */
    public R update(@RequestBody TdType tdType) {
        ValidatorUtils.validateEntity(tdType, UpdateGroup.class);
        typeService.update(tdType);
        return R.ok();
    }


    /**
     * 是否显示
     */
    public R updateState(@PathVariable int id, @PathVariable int typeOrder) {
        TdType tdType = typeService.queryById(id);
        tdType.setTypeorder(typeOrder);
        ValidatorUtils.validateEntity(tdType, UpdateGroup.class);
        typeService.update(tdType);
        return R.ok();
    }


    /**
     * 保存一级分类
     */
    @ApiOperation(value = "保存一级分类", notes = "")
    @ApiImplicitParam(name = "tdType", value = "一级分类", required = true, dataType = "TdType")
    @SysLog("保存商品")
    @RequestMapping("/save")
    @RequiresPermissions("fac:type:save")
    public R save(@RequestBody TdType tdType) {
        ValidatorUtils.validateEntity(tdType, AddGroup.class);
        if (tdType.getId() == null) {
            typeService.save(tdType);
        } else {
            typeService.update(tdType);
        }

        return R.ok();
    }


}
